// Requires Model to be loaded first
// View-relate

function View_init()
{
    initDocumentElements();
}

function initDocumentElements()
{
    $$('#Scrapbook li').each(function (elmt){
            Scrap_activateWrapper(elmt, false);
    });

    $$('#listBookmarks li .bookmark').each(function (elmt){
            Bookmark_activate(elmt);
    });

    $$('#Notebook .attachicon').each(function (elmt){
            activateIcon(elmt);
    });

    $('DocFilename').innerHTML = getCurrentFilename();
    new Proto.EIP( {type: 'text', item:$('DocFilename'), postSave: fileChange,
                autoselect:true} );

    $('DocTitle').innerHTML = document.title;
    new Proto.EIP( {type: 'text', item:$('DocTitle'), postSave: titleChange, autoselect:true} );

    $('textareaScrap').observe('keydown', NotePod_handlerKeyDown);
    $('textareaScrap').observe('keyup', NotePod_handlerKeyUp);

    setAddMode(gbAddMode);
    //$('textareaScrap').select();

    $$('.clipboard').each(function(elmt) {
        if(!gxClipboard)
            elmt.removeClassName('clipboard');
    });

    //Select a current line
    $$('.current').each(function(elmt) {
        if(elmt.hasClassName('scrap')) gxCurrentScrap = elmt; else elmt.removeClassName('current');
    });
    if(!gxCurrentScrap){
        gxCurrentScrap = $('Scrapbook').down('.scrap');
    }

    if(gxCurrentScrap){
            CurrentScrap_setNew(gxCurrentScrap, true);
            intelliScroll(gxCurrentScrap);
    }

    gbHideIcons? Icons_hide(): Icons_show();
    gbShowInfo? NoteInfo_show(): NoteInfo_hide();
    //TODO: Quick Hack for 0.70!
    //gbNumbering=!gbNumbering; Numbering_toggle(); //Unhack for 1.00

    NotebookList_render();
}

function displayNotebookInList(file)
{
    var addToLocation=$('listNotebooks');

    var notebook = document.createElement('a');
    notebook.addClassName('notebook');

    var item = document.createElement('li');

    var button = document.createElement('img');
    button.setAttribute('src', gsResDir + 'notebook.png');
    button.setAttribute('class', 'iconNotebook');
    button.setAttribute('title', 'Click for menu.');

    notebook.setAttribute('href', file);
    notebook.insert(file);
    item.insert({top:notebook});
    item.insert({top:button});
    addToLocation.insert({bottom:item});
}

function NotebookList_render()
{
    for(var i=0; i< gasNotebooks.length; i++)
        displayNotebookInList(gasNotebooks[i]);
}

function viewCreateScrap()
{
    var button = document.createElement('img');
    button.setAttribute('src',gsResDir + 'note.png');
    button.setAttribute('class','clicker');
    button.addClassName('view');

    button.setAttribute('title','Drag to move. Double-click to fold/expand.\nRight-click to copy formatting.SHIFT+right-click to apply formatting.');
    button.observe('dblclick',ScrapIcon_onDoubleClick);
    button.observe('mousedown', ScrapIcon_onMouseDown);
    button.observe('mouseup', ScrapIcon_onMouseUp);
    button.observe('contextmenu',ScrapIcon_onRightClick);

    if(Prototype.Browser.Opera)
        button.observe('mouseup', ScrapIcon_onDrop);
    else
        button.observe('mouseover', ScrapIcon_onDrop);

    return button;
}

function viewCreateControls()
{
    var controls = document.createElement('span');
    controls.addClassName('controls')

    var scrapClicker = document.createElement('img');
    scrapClicker.setAttribute('title','Click for Note menu.');
    scrapClicker.addClassName('iconActions');
    scrapClicker.addClassName('view');
    scrapClicker.setAttribute('src',gsResDir + 'down.png');

    var clipClicker = document.createElement('img');
    clipClicker.setAttribute('title','Click for Move menu.');
    clipClicker.addClassName('iconClip');
    clipClicker.addClassName('view');
    clipClicker.setAttribute('src',gsResDir + 'cut.gif');

    controls.insert({bottom:clipClicker})
    controls.insert({bottom:scrapClicker})

    return controls;
}

function foldNote(target){
    target.addClassName('minimized');
    target.immediateDescendants().each(function (elmt)
            {
                    if(elmt.hasClassName('commentlist')){
                                    elmt.hide();
                    }
            });
}

function expandNote(target)
{
    target.removeClassName('minimized');
    target.immediateDescendants().each(function (elmt)
            {
                    if(elmt.hasClassName('commentlist')){
                                    elmt.show();
                    }
            });
}

function Scrap_activateWrapper(wrapper, edit)
{
    var target= wrapper.down('.scrap');
    target.immediateDescendants().each(function (elmt)
            {
                if(elmt.hasClassName('content')) {
                    var x = new Proto.EIP( {type: 'textarea', item: elmt, 
                        postSave: contentChange,cols:40, postCancel: cancelEdit});
                    if(edit)
                            x.edit();
                    elmt.observe('click', ScrapContent_onClick);

                    if(Prototype.Browser.Opera)
                        elmt.observe('mouseup', Content_onDrop);
                    else
                        elmt.observe('mouseover', Content_onDrop);
                }
            });

   var iconScrap = viewCreateScrap();
   var iconControls = viewCreateControls();
   var iconCommentList = viewCreateCommentList();
   var barAdder = viewCreateAdder();

   target.insert({before:iconScrap});
   if(target.down('.commentlist')){
       target.insert({before:iconCommentList});
       iconCommentList.observe('contextmenu', CommentList_cycleNumbering)
   }

   target.down('.content').insert({after:iconControls});
   iconScrap.insert({before:barAdder})

    //TODO Doing brute-force for 0.40. Need to fine-tune
    if(gbHideIcons) Icons_hide();
    if(!gbInfoDisplay) NoteInfo_hide();
    if(gbNumbering) Numbering_on();
}

function View_unmakeScrapCurrent(){
    if(!gxCurrentScrap) return;
    if( gxCurrentScrap.up('.wrapper').previous('.baractions'))
        gxCurrentScrap.up('.wrapper').previous('.baractions').remove();
}

function View_makeScrapCurrent(element){
    var actions = document.createElement('div');
    actions.addClassName('baractions');
    actions.addClassName('view');

    //Moving back to menu-based. This creates jerky movement for cursor.
    if(gbQuickActions){
        actions.innerHTML = '<span class="actions">' +
            '<span onclick="CurrentScrap_addNote()">[Comment]</span>' +
            '<span onclick="CurrentScrap_edit()"> [Edit]</span>'+
            '<span onclick="CurrentScrap_bookmark()"> [Bookmark]</span>'+
            '<span onclick="CurrentScrap_info()"> [Info]</span>'+
            '</span>';
        actions.insert(' ');
    }

    var target= element.up('.wrapper');
    if(target)
        target.insert({before:actions});
}

function viewCreateCommentList(){
    var button = document.createElement('img');
    button.setAttribute('src',gsResDir + 'list.gif');
    button.setAttribute('class','foldindicator');
    button.setAttribute('title','Click for menu. Right-click to change numbering.');
    button.addClassName('view');
    return button;
}

function viewCreateAdder(){
    var bar = document.createElement('span');
    bar.addClassName('adder');
    bar.addClassName('view');
    bar.observe('click',adder_addNote);
    return bar;
}


function Scrap_getAttachIcons(scrap, bNoCreate){
    var list = null;
    var children = scrap.immediateDescendants();
    for(var i=0;i<children.length;i++){
        if(children[i].hasClassName('attachicons')){
            list = children[i];
            break;
        }
    }
    if(!list){
        if(bNoCreate) return list;
        list = document.createElement('span');
        list.addClassName('attachicons');
        list.insert(' ');
        scrap.down('.content').insert({after:list});
    }
    return list;
}

function contentChange(newtext,oldtext,elmt){
    var scrap = elmt.up('.scrap');
    if(scrap==gxCurrentScrap){
        if(gbAddMode=="eCurrentScrapEdit"){
            newtext = newtext.replace(/<br>/g,'\n');
            $('textareaScrap').value= newtext;
        }
    }
    flagDirty();
}